package util;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import pojo.Account;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Jwt {
    static private final String token="chat!=chat==chat";//密钥
    static private final int h=1;//时
    static private final int m=0;//分
    static private final int s=0;//秒
    static private Date delayTime;
    static {
        delayTime=new Date(System.currentTimeMillis()+(h*3600+m*60+s)*1000);
    }
    static synchronized public String secretKey(Account account){//加密

        return JWT.create()
                .withClaim("id",account.getId())
                .withExpiresAt(delayTime)
                .sign(Algorithm.HMAC256(token));

    }
    public synchronized static boolean decrypt(String message){//解密
        try{
            JWT.require(Algorithm.HMAC256(token)).build().verify(message);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    public synchronized static String getData(String message){ //获取用户在key保存的id
        String id=null;
        try{
            id= JWT.require(Algorithm.HMAC256(token))
                    .build()
                    .verify(message)
                    .getClaim("id").asString();

        }catch (Exception e){
            return null;
        }
        return id;
    }

}
